﻿<h1><a href="/"><strong>Cultiv</strong>Razor Examples</a></h1>
<ul>
    @* List all of the subnodes of the node at the first level that have a template *@
    @foreach (var node in Model.AncestorOrSelf(1).Children.Where("Template != 0")) {
        var isActive = false;
        if (Model.Id == node.Id || CurrentNodeIsChildOfSelectedNode(Model, node.Id)) {
            isActive = true;
        }
        <li><a href="@node.Url" class="@(isActive ? "current" : "")">@node.Name</a></li>
    }
</ul>

@functions
{ 
    public bool CurrentNodeIsChildOfSelectedNode(dynamic model, int selectedNodeId) {
        @*  
            Walk up the tree until there is no more parents left. 
            For each parent check if the Id equals the current Id. 
        *@
        while (model.Parent != null) {
            model = model.Parent;
            if (model.Id == selectedNodeId) {
                return true;
            }
        }

        return false;
    }
}